/*
 *  Copyright (C) 2010 Martin Haulrich <mwh.isv@cbs.dk>
 *
 *  This file is part of the MatrixParser package.
 *
 *  The MatrixParser program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.osdtsystem.matrixparser.featureextraction;

import org.osdtsystem.matrixparser.data.CONLLSentence;
import org.osdtsystem.matrixparser.data.CONLLToken;
import org.osdtsystem.matrixparser.features.FeatureVector;
import org.osdtsystem.matrixparser.parsers.ParsingModel;
import org.osdtsystem.matrixparser.parsers.output.DependencyTree;

/**
 *
 * @author Martin Haulrich
 */
public class FirstOrderExtractorLabelledNew extends AbstractFirstOrderExtractor implements FirstOrderExtractorLabelled {
    public FirstOrderExtractorLabelledNew(ParsingModel model) {
        super(model);
    }

    public void extract(FeatureVector fv, DependencyTree tree, int node) {
        int head = tree.head(node);
        extractLabelled(fv, tree.sentence(), tree.label(node), node,
                head < node, true);
        extractLabelled(fv, tree.sentence(), tree.label(node), head,
                head < node, false);
    }

    @Override
    public void extractLabelled(FeatureVector fv,
            CONLLSentence sentence,
            int label, int node, boolean RA, boolean child) {
        //FeatureVector fv = FeatureVectorFactory.makeNodeFeatureVector();
        String dir = RA ? "RA" : "LA";
        dir += "&" + child;
//        System.err.println(token + "  " + sentence.size());
//        System.err.println(sentence.get(0).get(CONLLToken.FORM));
        String w = sentence.get(node).get(CONLLToken.FORM);
        String wP = sentence.get(node).get(CONLLToken.POSTAG);

        String wPm1 = node > 0 ? sentence.get(node - 1).get(CONLLToken.POSTAG) : "STR";
        String wPp1 = node < sentence.size() - 1 ? sentence.get(node + 1).get(CONLLToken.POSTAG) : "END";

        String elabel = "" + label;
        add(fv, "NTS1=", elabel, dir);
        
        //fv.addValue(featureHandler.getFeature(feat), 1);

        String feat = "ANTS1=" + label;
        add(fv, "ANTS1=", elabel);
        //fv.addValue(featureHandler.getFeature(feat), 1);

        add(fv, "NTH=", w, wP, elabel);
        add(fv, "NTH=", w, wP, elabel, dir);

        add(fv, "NTI=", wP, elabel);
        add(fv, "NTI=", wP, elabel, dir);

        add(fv, "NTIA=", wPm1, wP, elabel);
        add(fv, "NTIA=", wPm1, wP, elabel, dir);

        add(fv, "NTIB=", wP, wPp1, elabel);
        add(fv, "NTIB=", wP, wPp1, elabel, dir);

        add(fv, "NTIC=", wPm1, wP, wPp1, elabel);
        add(fv, "NTIC=", wPm1, wP, wPp1, elabel, dir);

        add(fv, "NTJ=", w, elabel);
        add(fv, "NTJ=", w, elabel, dir);

        // We only want unknown counted once
        if (fv.getValue(featureHandler.UNKNOWN) != 0) {
            fv.setValue(featureHandler.UNKNOWN, 1);
        }
    }
}
